//
//  SDHSportsFileManager.m
//  SDHSportsNews
//
//  Created by MichaelShan on 2021/3/10.
//

#import "SDHSportsFileManager.h"

@implementation SDHSportsFileManager
static SDHSportsFileManager *_fileManager;

+ (instancetype)fileManager {
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
        //不能再使用alloc方法
        //因为已经重写了allocWithZone方法，所以这里要调用父类的分配空间的方法
        _fileManager = [[SDHSportsFileManager alloc] init];
    });

    return _fileManager;
}

// 防止外部调用alloc 或者 new
+ (instancetype)allocWithZone:(struct _NSZone *)zone {
    return [SDHSportsFileManager fileManager];
}

// 防止外部调用copy
- (id)copyWithZone:(nullable NSZone *)zone {
    return [SDHSportsFileManager fileManager];
}

// 防止外部调用mutableCopy
- (id)mutableCopyWithZone:(nullable NSZone *)zone {
    return [SDHSportsFileManager fileManager];
}

#pragma mark -
+ (void)saveDict:(NSDictionary *)dictionary toFileName:(NSString *)fileName {
    NSArray *array = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
    NSString *documents = [array objectAtIndex:0];
    NSString *documnetPath = [documents stringByAppendingPathComponent:fileName];
    
    NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithDictionary:dictionary];
    //去除空的value  否则不能写入到plist文件中去
    for (NSString *key in [dic allKeys]) {
        if ([[dic valueForKey:key] isEqual:[NSNull null]] || [dic valueForKey:key] == nil) {
            [dic removeObjectForKey:key];
        }
    }
    //将字典存入指定的本地文件
    [dic writeToFile:documnetPath atomically:YES];
//    NSLog(@"沙盒文件路径：%@", documnetPath);
    
}

+ (NSDictionary *)readDataFromFileName:(NSString *)fileName {
    NSArray *array = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
    NSString *documents = [array objectAtIndex:0];
    NSString *documnetPath = [documents stringByAppendingPathComponent:fileName];
    
    NSDictionary *userDic;
    if([[NSFileManager defaultManager] fileExistsAtPath:documnetPath]){
       userDic = [[NSDictionary alloc] initWithContentsOfFile:documnetPath];
    }else{
        return nil;
    }
    return userDic;
}

+ (void)saveArray:(NSArray *)array toFileName:(NSString *)fileName {
    NSArray *pathArray = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
    NSString *documents = [pathArray objectAtIndex:0];
    NSString *documnetPath = [documents stringByAppendingPathComponent:fileName];
    
    //将字典存入指定的本地文件
    [array writeToFile:documnetPath atomically:YES];
}

+ (NSArray *)readArrayFromFileName:(NSString *)fileName {
    NSArray *array = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
    NSString *documents = [array objectAtIndex:0];
    NSString *documnetPath = [documents stringByAppendingPathComponent:fileName];
    
    NSArray *tmpArray = nil;
    if([[NSFileManager defaultManager] fileExistsAtPath:documnetPath]){
        tmpArray = [[NSArray alloc] initWithContentsOfFile:documnetPath];
    }else{
        return nil;
    }
    return tmpArray;
}

+ (void)deleteDataWithFileName:(NSString *)fileName {
    NSArray *array = NSSearchPathForDirectoriesInDomains(NSCachesDirectory, NSUserDomainMask, YES);
    NSString *documents = [array objectAtIndex:0];
    NSString *documnetPath = [documents stringByAppendingPathComponent:fileName];
    [[NSFileManager defaultManager] fileExistsAtPath:documnetPath];
    
}

@end
